package best.driver.android.Activity.Order.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.Activity.Register.CheckActivity;
import best.driver.android.Beans.CarCheckInfo;
import best.driver.android.Beans.CarItemInfo;
import best.driver.android.Beans.OrderListItemInfo;
import best.driver.android.Event.SelectCarEvent;
import best.driver.android.R;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

import java.util.ArrayList;
import java.util.List;

public class CarItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private List<CarCheckInfo> mCarItems = new ArrayList<CarCheckInfo>();

    public CarItemAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<CarItemInfo> list) {
        mCarItems.clear();
        if(list == null || list.size() == 0) {
            return;
        }
        for(int i = 0; i < list.size(); i++) {
            CarItemInfo info = list.get(i);
            if(info != null && info.eachDetail.size() > 0) {
                for(int j = 0; j < info.eachDetail.size(); j++) {
                    CarCheckInfo checkInfo = new CarCheckInfo();
                    checkInfo.truckName = info.eachDetail.get(j);
                    checkInfo.type = info.type;
                    checkInfo.vehicleId = info.vehicleId;
                    mCarItems.add(checkInfo);
                }
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mCarItems.size();
    }

    @Override
    public CarCheckInfo getItem(int position) {
        return mCarItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_select_car, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent();
//                intent.putExtra("car_info", mCarItems.get(position));
//                ((Activity) mContext).setResult(CheckActivity.RESULT_OK, intent);
//                ((Activity) mContext).finish();
                SelectCarEvent event = new SelectCarEvent(getItem(position));
                EventBus.getDefault().post(event);
                ((Activity) mContext).finish();
            }
        });
        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        CarCheckInfo item = getItem(position);
        holder.mTvCarType.setText(item.truckName);
        holder.mTvTypeTitle.setText(item.type);
        if(position == 0) {
            holder.mLinearCarTop.setVisibility(View.VISIBLE);
        }
        if(position > 0) {
            CarCheckInfo lastItem = getItem(position - 1);
            if(lastItem.vehicleId.equals(item.vehicleId)) {
                holder.mLinearCarTop.setVisibility(View.GONE);
            } else {
                holder.mLinearCarTop.setVisibility(View.VISIBLE);
            }
        }
    }

    static class ViewHolder {

        @InjectView(R.id.ll_car_top)
        LinearLayout mLinearCarTop;

        @InjectView(R.id.tv_car_type)
        TextView mTvCarType;

        @InjectView(R.id.tv_type_title)
        TextView mTvTypeTitle;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
